package com.se.xb.controller;

import com.alibaba.fastjson.JSON;
import com.se.xb.entity.Dept;
import com.se.xb.entity.User;
import com.se.xb.entity.dto.DeptDto;
import com.se.xb.entity.dto.PageResult;
import com.se.xb.entity.dto.Result;
import com.se.xb.entity.dto.SearchDto;
import com.se.xb.service.DeptService;
import com.se.xb.service.UserService;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@WebServlet("/dept/*")
public class DeptServlet extends BaseServlet{

    DeptService deptService = new DeptService();

    UserService userService = new UserService();


    public void getDeptList(HttpServletRequest request, HttpServletResponse response) throws Exception {
        List<User> userList = userService.getUserList();
        List<Dept> deptList = deptService.getDeptList();
        DeptDto deptDto = new DeptDto();
        Map<String, List<User>> deptmap = new HashMap<>();
        for (Dept dept : deptList) {
            ArrayList<User> users = new ArrayList<>();
            for (User user : userList){
                if (dept.getId().equals(user.getDeptId())){
                    users.add(user);
                }
            }
            deptmap.put(dept.getName(),users);
        }
        deptDto.setDeptmap(deptmap);
        response.getWriter().write(JSON.toJSONString(Result.success(deptDto)));
    }
}
